package com.tyd.cams;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.tyd.cams.Adapter.TzggAdapter;
import com.tyd.cams.entity.SkTzgg;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;

import okhttp3.Call;

import static com.xuexiang.xui.utils.ResUtils.getResources;

public class TzggActivity extends AppCompatActivity {
    //定义一个变量
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tzgg);
        //先取出Intent，再取出Intent里面的数据
        Intent intent = getIntent();
        String url = intent.getStringExtra("href");
        getggxq(url);
//        webpage(); 测试WebPage

    }

    /*通知公告详情List获取开始*/
    private void getggxq(String url) {
        new Thread() {
            public void run() {
                OkHttpUtils.post()
                        .url(url)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(TzggActivity.this, "通知公告链接服务器超时", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                Document document = Jsoup.parse(response);
//                                Elements context = document.select("body > div.wrap > div.rbox > div.rboxa");
                                Elements context = document.select("body > div.main > div.p-r > div.mainbox");
                               if(context.size()==0){
                                    context = document.select("body > div.Tbox > div.Tbox_R > div.Nbox > div.mainbox");
                               }
                                String title = context.select("div.textbox > h1").text();
                                String ydccs = document.select("body > div.wrap > div.rbox > div.rboxa > div.N2title > div.N2cs").text();
                                System.out.println("阅读次数" + ydccs);
                                String sjandlaiyuan = context.select("div.textbox > div").text();
                                String cont = context.select("div.textbox").html();
                                TextView tzggtitle = findViewById(R.id.Tzggtitle);
                                TextView yueducishu = findViewById(R.id.yueducishu);
                                TextView laiyuan = findViewById(R.id.laiyuan);
                                TextView Tzggcontext = findViewById(R.id.Tzggcontext);
                                /*内容*/
                                /*Tzggcontext.setText(Html.fromHtml(cont));*/
                                Spanned sp = Html.fromHtml(cont, new Html.ImageGetter() {
                                    @Override
                                    public Drawable getDrawable(String cont) {
                                        InputStream is = null;
                                        try {
                                            is = (InputStream) new URL(cont).getContent();
                                            Drawable d = Drawable.createFromStream(is, "src");
                                            d.setBounds(0, 0, d.getIntrinsicWidth(),
                                                    d.getIntrinsicHeight());
                                            is.close();
                                            return d;
                                        } catch (Exception e) {
                                            return null;
                                        }
                                    }
                                }, null);
                                Tzggcontext.setText(sp);
                                Tzggcontext.setMovementMethod(ScrollingMovementMethod.getInstance());//段落文本的话要加这个
                                /*标题*/
                                tzggtitle.setText(Html.fromHtml(title));
                                /*阅读次数*/
                                yueducishu.setText(ydccs);
                                /*来源*/
                                laiyuan.setText(sjandlaiyuan);

                                Log.i("阅读次数", ydccs);
                            }

                            /*Log.i("通知公告信息",context.toString());*/
                        });
            }
        }.start();

    }

   /* private void webpage() { //先取出Intent，再取出Intent里面的数据
        Intent intent = getIntent();
        String url = intent.getStringExtra("href");

        mWebView = (WebView) findViewById(R.id.web_view);
        //让网络浏览的客户端设定在当前的WebView控件上（防止跳转到原生浏览器）
        mWebView.setWebViewClient(new WebViewClient() {
            //开始加载网页的回调方法
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            //网页加载结束的回调方法
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //如果等待窗口还在显示，那么关闭
            }

        });
        //开启JavaScript
        mWebView.getSettings().setJavaScriptEnabled(true);
        //加载网页
        mWebView.loadUrl(url);
    }*/
}
